package com.forum.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.forum.common.model.entity.Favorite;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface FavoriteMapper extends BaseMapper<Favorite> {
    
    /**
     * 查询用户是否已收藏
     * @param userId 用户ID
     * @param postId 帖子ID
     * @return 收藏记录
     */
    @Select("SELECT * FROM favorites WHERE user_id = #{userId} AND post_id = #{postId}")
    Favorite findByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
    
    /**
     * 删除收藏记录
     * @param userId 用户ID
     * @param postId 帖子ID
     * @return 影响行数
     */
    @Delete("DELETE FROM favorites WHERE user_id = #{userId} AND post_id = #{postId}")
    int deleteByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
    
    /**
     * 获取帖子收藏数量
     * @param postId 帖子ID
     * @return 收藏数量
     */
    @Select("SELECT COUNT(*) FROM favorites WHERE post_id = #{postId}")
    int countByPostId(@Param("postId") Long postId);
    
    /**
     * 判断用户是否已收藏
     * @param userId 用户ID
     * @param postId 帖子ID
     * @return 是否已收藏
     */
    @Select("SELECT COUNT(*) FROM favorites WHERE user_id = #{userId} AND post_id = #{postId}")
    int existsByUserIdAndPostId(@Param("userId") Long userId, @Param("postId") Long postId);
    
    /**
     * 获取用户收藏的帖子ID列表
     * @param userId 用户ID
     * @return 帖子ID列表
     */
    @Select("SELECT post_id FROM favorites WHERE user_id = #{userId} ORDER BY created_at DESC")
    List<Long> selectPostIdsByUserId(@Param("userId") Long userId);
} 